package com.hh.service.service.impl.message.consumer;

import com.hh.common.config.RabbitMqConfig;
import com.hh.common.dto.RegisterImAccountDTO;
import com.hh.service.service.im.ImUserService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class UserImConsumerMessage {

    @Autowired
    private ImUserService imUserService;

    /**
     * 消费用户注册IM账号消息
     * @param message
     * @param registerImAccountDTO
     * @param channel
     */
    @RabbitListener(queues = {RabbitMqConfig.IM_USER_REGISTER_QUEUE})
    public void registerImAccount(Message message, RegisterImAccountDTO registerImAccountDTO, Channel channel) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String imUserId = registerImAccountDTO.getImUserId();
        String avatar = registerImAccountDTO.getAvatar();
        String nickname = registerImAccountDTO.getNickname();
        //注册IM账号,并自动拉进群
        boolean b = imUserService.registerImAccount(imUserId, avatar, nickname);
        if (b) {
            try {
                channel.basicAck(deliveryTag, false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
